﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{
    class Telhado : GameObject3D
    {
        VertexPositionTexture[] vertstelhado;
        VertexBuffer vertexBuffer;
        BasicEffect effect;

        public Telhado(float size, GraphicsDevice graphics, Camera camera, Vector3 position, Texture2D textura)
        {
            vertstelhado = new VertexPositionTexture[14];
            vertstelhado[0] = new VertexPositionTexture(new Vector3(size + position.X, position.Y, size + position.Z), new Vector2(0,0));
            vertstelhado[1] = new VertexPositionTexture(new Vector3(-size + position.X,  position.Y, size + position.Z), new Vector2(1, 0));
            vertstelhado[2] = new VertexPositionTexture(new Vector3(size + position.X, position.Y, -size + position.Z), new Vector2(0, 1));
            vertstelhado[3] = new VertexPositionTexture(new Vector3(-size + position.X, position.Y, -size + position.Z), new Vector2(1, 1));
            vertstelhado[4] = new VertexPositionTexture(new Vector3(0 + position.X, 0.9f *size + position.Y, 0 + position.Z), new Vector2(0, 0));
            vertstelhado[5] = new VertexPositionTexture(new Vector3(-size + position.X, position.Y, size + position.Z), new Vector2(1, 0));
            vertstelhado[6] = new VertexPositionTexture(new Vector3(size + position.X, position.Y, size + position.Z), new Vector2(0, 1));
            vertstelhado[7] = new VertexPositionTexture(new Vector3(size + position.X,  position.Y, -size + position.Z), new Vector2(1, 1));
            vertstelhado[8] = new VertexPositionTexture(new Vector3(0 + position.X, 0.9f * size + position.Y, 0 + position.Z), new Vector2(0, 0));


            vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionTexture), vertstelhado.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture>(vertstelhado);

            effect = new BasicEffect(graphics);
            effect.Texture = textura; ;
            effect.TextureEnabled = true;
        }
        public override void Update(Camera camera)
        {
            effect.View = camera.view;
            effect.Projection = camera.projection;
            effect.World = Matrix.Identity;

            base.Update(camera);
        }
        public override void Draw(GraphicsDevice graphics)
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphics.SetVertexBuffer(vertexBuffer);
                graphics.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, vertstelhado, 0, 7);
            }
            base.Draw(graphics);
        }
    }
}
